<template>
	<!--左侧导航-->
	<aside :class="{ showSidebar: !collapsed }">
		<!--展开折叠开关-->
		<div class="menu-toggle" @click.prevent="collapse">
			<i class="el-icon-s-fold" v-show="!collapsed" title="收起"></i>
			<i class="el-icon-s-unfold" v-show="collapsed" title="展开"></i>
		</div>
		<!--导航菜单-->
		<el-menu :default-active="setpageindex" router :collapse="collapsed" ref="leftNavigation">
			<template v-for="issue in routerList">
				<!-- 注意：这里就是leftNavState状态作用之处，当该值与router的根路由的name相等时加载相应菜单组 -->
				<template v-if="issue.entity.name === $store.state.leftNavState">
					<template v-for="(item, index) in issue.children">
						<el-submenu v-if="!item.leaf" :index="index + ''" v-show="item.entity.menuShow" :key="index">
							<template slot="title"
								><i :class="item.entity.iconCls"></i><span slot="title">{{ item.entity.alias }}</span></template
							>
							<el-menu-item
								v-for="term in item.children"
								:key="term.path"
								:index="term.path"
								:class="$route.path === term.path ? 'is-active' : ''"
							>
								<i :class="term.iconCls"></i><span slot="title">{{ term.entity.alias }}</span>
							</el-menu-item>
						</el-submenu>
						<el-menu-item
							v-else-if="item.leaf"
							:index="item.path"
							:class="$route.path === item.entity.path ? 'is-active' : ''"
							v-show="item.entity.menuShow"
							:key="index"
						>
							<i :class="item.iconCls"></i><span slot="title">{{ item.entity.alias }}</span>
						</el-menu-item>
					</template>
				</template>
			</template>
		</el-menu>
	</aside>
</template>
<script>
export default {
	name: 'leftNav',
	data() {
		return {
			loading: false,
			collapsed: this.$store.state.collapsed,
			pathVal: this.$store.state.leftNavState,
			routerList : JSON.parse(window.sessionStorage.getItem('routerlist')) ,
			setpageindex: '/'
		}
	},
	methods: {
		//折叠导航栏
		collapse: function () {
			this.collapsed = !this.collapsed
			this.$store.state.collapsed = this.collapsed
		},
		// 左侧导航栏根据当前路径默认打开子菜单（如果当前是二级菜单，则父级子菜单默认打开）
		defaultLeftNavOpened() {
			let cur_path = this.$route.path //获取当前路由
			// let routers = this.$router.options.routes // 获取路由对象
			let routers = this.routerList
			console.log('sddddbbbb', this.$router.options.routes);
			let subMenuIndex = '',
				needOpenSubmenu = false
			for (let i = 0; i < routers.length; i++) {
				let children = routers[i].children
				if (children) {
					for (let j = 0; j < children.length; j++) {
						if (children[j].entity.path === cur_path) {
							break
						}
						// 如果该菜单下还有子菜单
					console.log(13123, children[j]	);
						if (children[j].children && !children[j].leaf) {
							console.log('aaaaa')
							let grandChildren = children[j].children
							for (let z = 0; z < grandChildren.length; z++) {
								if (grandChildren[z].entity.path === cur_path) {
									subMenuIndex = j
									needOpenSubmenu = true
									break
								}
							}
						}
					}
				}
			}
			if (this.$refs['leftNavigation'] && needOpenSubmenu) {
				console.log('wolaile')
				this.$refs['leftNavigation'].open(subMenuIndex) // 打开子菜单
			}
		},
	},
	watch: {
		$route(to, from) {
			console.log('top' + to, 'from' + from)
			console.log('ggggg', this.$route)
			// _this.defaultLeftNavOpened() //加载左侧菜单-监听
		},
	},
	mounted() {
		console.log('sss111', this.routerList );
		// this.routerList = 
		this.defaultLeftNavOpened()
	},
}
</script>
<style lang="less" scoped></style>
